package javagprssrv.DataAnalyse;

import java.io.Reader;
import java.io.FileReader;
import javagprssrv.JGS_Param;
import java.io.File;
import java.util.Vector;
import javax.script.CompiledScript;
import javax.script.Compilable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class ScriptExecuter
{
    private java.lang.String ScriptFileName;
    private javax.script.ScriptEngineManager seManager;
    private javax.script.ScriptEngine se;
    private javax.script.Compilable compilable;
    private javax.script.CompiledScript cs;
    private java.util.Vector ParamName;
    private java.util.Vector ParamValue;
    
    public ScriptExecuter(final java.lang.String _ScriptFileName) throws java.lang.Exception {
        this.ScriptFileName = "";
        this.ParamName = new java.util.Vector();
        this.ParamValue = new java.util.Vector();
        this.ScriptFileName = _ScriptFileName;
        final java.io.File f = new java.io.File(this.ScriptFileName);
        if (!f.exists()) {
            javagprssrv.JGS_Param.myLog.ShowLog("没有找到脚本文件“" + this.ScriptFileName + "”", java.lang.Boolean.TRUE, java.lang.Boolean.TRUE);
            throw new java.lang.Exception("没有找到脚本文件“" + this.ScriptFileName + "”");
        }
        this.seManager = new javax.script.ScriptEngineManager();
        this.se = this.seManager.getEngineByName("JavaScript");
        this.compilable = (javax.script.Compilable)this.se;
        try {
            this.cs = this.compilable.compile(new java.io.FileReader(this.ScriptFileName));
        }
        catch (java.lang.Exception e) {
            javagprssrv.JGS_Param.myLog.ShowLog("脚本文件 " + this.ScriptFileName + " 中存在错误,请确认 ！！！", java.lang.Boolean.TRUE, java.lang.Boolean.TRUE);
        }
    }
    
    public java.lang.Object Execute(final int intFunctionIndex, final java.lang.String strFunctionParameter) {
        java.lang.Object res = new java.lang.Object();
        this.getParamFromParamString(strFunctionParameter);
        this.se.put("FunctionIndex", intFunctionIndex);
        for (int i = 0; i < this.ParamName.size(); ++i) {
            this.se.put(java.lang.String.valueOf(this.ParamName.elementAt(i)), java.lang.String.valueOf(this.ParamValue.elementAt(i)));
        }
        try {
            this.cs.eval();
        }
        catch (java.lang.Exception e) {
            e.printStackTrace();
        }
        res = this.se.get("RESULT");
        return res;
    }
    
    public java.lang.String ExecuteReturnStringValue(final int intFunctionIndex, final java.lang.String strFunctionParameter, final java.lang.String strReturnName) {
        java.lang.String res = "";
        this.getParamFromParamString(strFunctionParameter);
        this.se.put("FunctionIndex", intFunctionIndex);
        for (int i = 0; i < this.ParamName.size(); ++i) {
            this.se.put(java.lang.String.valueOf(this.ParamName.elementAt(i)), java.lang.String.valueOf(this.ParamValue.elementAt(i)));
        }
        try {
            this.cs.eval();
        }
        catch (java.lang.Exception e) {
            e.printStackTrace();
        }
        res = java.lang.String.valueOf(this.se.get(strReturnName));
        return res;
    }
    
    public java.util.Vector Execute(final int intFunctionIndex, final java.lang.String strFunctionParameter, final java.lang.String strReturnName) {
        final java.util.Vector res = new java.util.Vector();
        this.getParamFromParamString(strFunctionParameter);
        this.se.put("FunctionIndex", intFunctionIndex);
        for (int i = 0; i < this.ParamName.size(); ++i) {
            this.se.put(java.lang.String.valueOf(this.ParamName.elementAt(i)), java.lang.String.valueOf(this.ParamValue.elementAt(i)));
        }
        try {
            this.cs.eval();
        }
        catch (java.lang.Exception e) {
            e.printStackTrace();
        }
        final java.util.Vector V1 = this.getReturnName(strReturnName);
        for (int j = 0; j < V1.size(); ++j) {
            java.lang.Object o_res = new java.lang.Object();
            o_res = this.se.get(java.lang.String.valueOf(V1.elementAt(j)));
            res.add(o_res);
        }
        V1.clear();
        return res;
    }
    
    public void PUT(final java.lang.String ObjectName, final java.lang.Object OBJ) {
        this.se.put(ObjectName, OBJ);
    }
    
    public java.lang.Object GET(final java.lang.String ObjectName) {
        java.lang.Object o = new java.lang.Object();
        o = this.se.get(ObjectName);
        return o;
    }
    
    private java.util.Vector getReturnName(java.lang.String strReturnName) {
        final java.util.Vector res = new java.util.Vector();
        final java.lang.String SP1 = ",";
        while (strReturnName.length() > 0) {
            final int idx = strReturnName.indexOf(SP1);
            if (idx > -1) {
                final java.lang.String s = strReturnName.substring(0, idx).trim();
                if (s.length() > 0) {
                    res.add(s);
                }
                strReturnName = strReturnName.substring(idx + 1, strReturnName.length());
            }
            else {
                final java.lang.String s = strReturnName;
                if (s.length() > 0) {
                    res.add(s);
                }
                strReturnName = "";
            }
        }
        return res;
    }
    
    private void getParamFromParamString(java.lang.String strFunctionParameter) {
        java.lang.String name = "";
        java.lang.String value = "";
        java.lang.String subStr = "";
        int idx = -1;
        int idx2 = -1;
        final java.lang.String SP1 = ",";
        final java.lang.String SP2 = "=";
        this.ParamName.clear();
        this.ParamValue.clear();
        while (strFunctionParameter.length() > 0) {
            idx = strFunctionParameter.indexOf(SP1);
            if (idx > -1) {
                subStr = strFunctionParameter.substring(0, idx);
                if (subStr.length() == 0) {
                    strFunctionParameter = strFunctionParameter.substring(idx + 1, strFunctionParameter.length());
                }
                else {
                    idx2 = subStr.indexOf(SP2);
                    if (idx2 == -1) {
                        strFunctionParameter = strFunctionParameter.substring(idx + 1, strFunctionParameter.length());
                    }
                    else {
                        name = subStr.substring(0, idx2);
                        value = subStr.substring(idx2 + 1, subStr.length());
                        this.ParamName.add(name);
                        this.ParamValue.add(value);
                        strFunctionParameter = strFunctionParameter.substring(idx + 1, strFunctionParameter.length());
                    }
                }
            }
            else {
                subStr = strFunctionParameter;
                idx2 = subStr.indexOf(SP2);
                if (idx2 == -1) {
                    strFunctionParameter = "";
                }
                else {
                    name = subStr.substring(0, idx2);
                    value = subStr.substring(idx2 + 1, subStr.length());
                    this.ParamName.add(name);
                    this.ParamValue.add(value);
                    strFunctionParameter = "";
                }
            }
        }
    }
}
